package com.noble.glamour.ui.user.my.wallet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.noble.glamour.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.base.BaseActivity;
import com.noble.glamour.module.logic.model.impl.UnreadMgrImpl;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.log.PSP;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;

/**
 * 我的钱包页面
 */
public class RedBoxRecordAct extends BaseActivity implements PObserver, View.OnClickListener {
    public static String REDBOXMONEY = "REDBOXMONEY";//键

    private TextView tvMoney;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.f1_wode_red_bag_record_act);
        MsgMgr.getInstance().attach(this);
        if (!ModuleMgr.getCommonMgr().getIdCardVerifyStatusInfo().getIsVerifyIdCard()) {
            ModuleMgr.getCommonMgr().getVerifyStatus(null);
        }
        initView();
        ModuleMgr.getUnreadMgr().resetUnreadByKey(UnreadMgrImpl.MY_WALLET);
    }

    private void initView() {
        initTitle();
        tvMoney = (TextView) findViewById(R.id.wode_wallet_tv_money);
        findViewById(R.id.wallet_draw).setOnClickListener(this);
        findViewById(R.id.wallet_declare).setOnClickListener(this);
        refreshView(ModuleMgr.getCenterMgr().getMyInfo().getRedbagsum() / 100f);
    }

    private void initTitle() {
        setTitleBackground(getResources().getColor(R.color.white));
        setBackView(true);
        setTitle(getString(R.string.wallet_mine), getResources().getColor(R.color.black));
        setTitleRight(getString(R.string.wallet_detail), getResources().getColor(R.color.text_ciyao_gray), this);
    }

    @Override
    protected boolean isImmersionBar() {
        return false;
    }

    public void refreshView(double money) {
        PSP.getInstance().put(REDBOXMONEY + ModuleMgr.getCenterMgr().getMyInfo().getUid(), (float) money);//存储可提现金额
        tvMoney.setText(money + "");
        ModuleMgr.getCenterMgr().getMyInfo().setRedbagsum(money * 100);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wallet_draw:      // 提现
                String money = tvMoney.getText().toString().trim();
                float minMoney = ModuleMgr.getCommonMgr().getCommonConfig().getPay().getMin_withdraw_money() / 100f;
                if (Float.valueOf(money) <= minMoney) {
                    PToast.showShort(getString(R.string.withdraw_tips) + minMoney + getString(R.string.head_unit));
                    return;
                }

                int status = ModuleMgr.getCommonMgr().getIdCardVerifyStatusInfo().getStatus();
                if (!ModuleMgr.getCenterMgr().getMyInfo().isVerifyCellphone()) {//是否绑定了手机号
                    PToast.showShort(getString(R.string.wallet_auth_phone));
                    break;
                }
                if (status <= 0) {//是否进行了身份认证
                    PToast.showShort(getString(R.string.wallet_auth_card));
                    break;
                }
                if (status != 2) {//身份认证还未通过
                    PToast.showShort(R.string.the_identity_certification_audit);
                    break;
                }
                UIShow.showWithDrawApplyAct(0, 0, false, this);
                break;

            case R.id.wallet_declare:
                UIShow.showWithDrawExplainAct(this);   //跳转到提现说明页
                break;

            case R.id.base_title_right_txt:
                PToast.showShort("收入明细");
                break;
            default:
                break;
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_GET_MONEY_Notice:
                refreshView(0.0); //提现成功更新可提现金额
                break;
            default:
                break;
        }
    }
}